package kr.hkit.myframework.db;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;

public class DBProperties {
	
	public static Properties loProperties(String propertiesFile){
		BufferedInputStream bis=null;
		Properties prop=null;
		try {
			bis=new BufferedInputStream(new FileInputStream(propertiesFile));
			prop=new Properties();
			prop.loadFromXML(bis);
		} catch (FileNotFoundException e) {
			System.err.println("파일이 존재하지 않습니다");
			e.printStackTrace();
		} catch (InvalidPropertiesFormatException e) {
			System.err.println("유효하지 않은 파일");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(bis!=null)try{bis.close();bis=null;}catch (Exception e) {
			}
		}
		
		return prop;
		
		
	}
	// savePropertis("test.prof',"user","pass","db");
	public static void savePropertis(String propertiesFile, String... values) {
		BufferedOutputStream bos = null;
		Properties prop = null;
		try {
			bos = new BufferedOutputStream(new FileOutputStream(propertiesFile));
			prop = new Properties();// 정보를 알기위해서
			// 정보넣기
			prop.setProperty("DRIVER", values[0]);
			prop.setProperty("URL", values[1]);
			prop.setProperty("USER", values[2]);
			prop.setProperty("PASSWORD", values[3]);

			prop.storeToXML(bos, "DataBase Connection XML Property File");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (bos != null)
				try {
					bos.close();
					bos = null;
				} catch (Exception e) {
				}
		}
	}
		public static void prnProperties(Properties prop){
			Set<Entry<Object, Object>> setmap = prop.entrySet();
			
			for (Entry<Object, Object> propty : setmap){
				System.out.printf("%s : %s\n", propty.getKey(), propty.getValue());
			}
		}
	
	}

